package edu.hcmus.sow.dao;

import java.util.Calendar;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.Project;
import edu.hcmus.sow.domain.TaskWorkLog;
import edu.hcmus.sow.domain.User;

/**
 * DAO to manage TaskWorkLog entities.
 * 
 */
@Repository("TaskWorkLogDAO")
@Transactional
public class TaskWorkLogDAOImpl extends AbstractHibernateDAO<TaskWorkLog, Integer> implements TaskWorkLogDAO {
	public List<TaskWorkLog> getListsByTaskID (Integer taskID)throws DataAccessException{
		String queryString = "select twl from TaskWorkLog twl " +
				"join fetch twl.task t " +
				"join fetch twl.user tu " +
				"where t.id = ?";
		List<TaskWorkLog> list = executeQuery(queryString, taskID);
		return list;
	}
	
	public List<TaskWorkLog> getTaskWorkLogReports(User user, Project project, Calendar from, Calendar to)throws DataAccessException{
	   String queryString = "select twl from TaskWorkLog twl " +
	   		"join fetch twl.task t " +
	   		"join fetch twl.task.project tpro " +
	   		"join fetch twl.task.taskCategory tcat " +
	   		"join fetch twl.user tu" +
	   		
	   		" WHERE twl.user = ? " +
	   		" AND tpro = ? " +
	   		" AND DATEDIFF(twl.loggedDate, ?) >= 0" +
	   		" AND DATEDIFF(?, twl.loggedDate) >= 0";
      List<TaskWorkLog> list = executeQuery(queryString, user, project, from.getTime(), to.getTime());
      return list;
	}
}
